package com.yuwei.home

import android.os.Build
import android.util.Log
import android.view.View
import android.view.View.OnScrollChangeListener
import androidx.annotation.RequiresApi
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.tencent.smtt.utils.s
import com.youth.banner.adapter.BannerAdapter
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import com.youth.banner.indicator.CircleIndicator
import com.yuwei.base.BaseFragment
import com.yuwei.common.entity.MenuEntity
import com.yuwei.base.path.RouterPath
import com.yuwei.home.adapter.HotServiceAdapter
import com.yuwei.home.databinding.FragmentHomeBinding

@Route(path = RouterPath.MODULE_HOME_FRAGMENT)
class HomeFragment : BaseFragment<FragmentHomeBinding, HomeViewModel>() {
    val homeNoticeAdapter = com.yuwei.home.adapter.HomeNoticeAdapter()
    private var page = 0
    private val list = mutableListOf<MenuEntity>().apply {
        // 第一行
        add(MenuEntity(R.drawable.home_menu_image1, "车位管理"))      // 粉色停车标识
        add(MenuEntity(R.drawable.home_menu_image2, "访客登记"))     // 蓝底白字登记牌
        add(MenuEntity(R.drawable.home_menu_image3, "物业报修"))     // 橙色工具图标
        add(MenuEntity(R.drawable.home_menu_image4, "考勤打卡")) // 时钟图标

        // 第二行
        add(MenuEntity(R.drawable.home_menu_image5, "房源信息"))       // 带绿树的房屋
        add(MenuEntity(R.drawable.home_menu_image6, "商家推荐"))      // 店铺图案
        add(MenuEntity(R.drawable.home_menu_image7, "月卡会员"))        // VIP字样卡片
        add(MenuEntity(R.drawable.home_menu_image8, "设备管理"))    // 带百分比的齿轮
    }
    private val homeMenuAdapter = com.yuwei.home.adapter.HomeMenuAdapter()
    private val list1 = mutableListOf<Int>().apply {
        add(R.drawable.banner1)
        add(R.drawable.banner2)
    }
    lateinit var hotServiceAdapter : HotServiceAdapter
    @RequiresApi(Build.VERSION_CODES.M)
    override fun initView() {
        binding.homeRv.layoutManager = LinearLayoutManager(requireContext())
        //RecyclerView 自定义适配器
        loadData()
        hotServiceAdapter = HotServiceAdapter(list)
        binding.homeMenuRv.adapter = hotServiceAdapter
        homeMenuAdapter.data.clear()
        //万能适配器
        binding.homeRv.adapter = homeNoticeAdapter
        homeMenuAdapter.data.addAll(list)
        homeMenuAdapter.notifyDataSetChanged()
        binding.homeMenuRv.adapter = homeMenuAdapter
        binding.homeMenuRv.layoutManager = GridLayoutManager(requireContext(), 4)
        homeMenuAdapter.setOnItemClickListener(object : OnItemClickListener{
            override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
                when(position){
                    0->{
                        ARouter.getInstance().build(RouterPath.MODULE_PARKING_LOT_ACTIVITY).navigation()
                    }
                    1->{
                        ARouter.getInstance().build(RouterPath.MODULE_VISIT_ACTIVITY).navigation()
                    }
                    3->{
                       ARouter.getInstance().build(RouterPath.MODULE_SIGN_ACTIVITY).navigation()
                    }
                    4->{
                        ARouter.getInstance().build(RouterPath.MODULE_PROPERTY_ACTIVITY).navigation()
                    }
                    7->{
                        ARouter.getInstance().build(RouterPath.MODULE_DEVICE_ACTIVITY).navigation()
                    }
                }
            }

        })

        binding.textBanner.setDatas(listOf(
            "灌灌灌灌",
            "强强强强",
            "哈哈哈哈",
            "怕怕怕怕"
        ))
        binding.textBanner.startViewAnimator()
        binding.homeBanner.setAdapter(
            object : BannerImageAdapter<Int>(list1){
            override fun onBindView(
                holder: BannerImageHolder,
                data: Int,
                position: Int,
                size: Int
            ) {
                Glide.with(requireActivity()).load(data).into(holder.imageView)
            }

        })
        binding.homeBanner.indicator = CircleIndicator(requireContext())
        binding.homeBanner.addBannerLifecycleObserver(this.activity)
        binding.nsv.run {
           setOnScrollChangeListener { v, _, scrollY, _, _ ->
               if(scrollY == (binding.layoutView.measuredHeight - v.measuredHeight)){
                   page+=1
                   loadData()
               }
           }
        }
    }
    fun loadData(){
        viewModel.getNotice(page,5)
    }
    override fun initData() {
        viewModel.getNoticeSuccess.observe(this){
            if(it.size == 0){
                ToastUtils.showShort("没有更多了")
                return@observe
            }
            homeNoticeAdapter.data.addAll(it)
            homeNoticeAdapter.notifyDataSetChanged()
        }
    }

}